import mongoengine as db
import arrow

from app.db import BaseDocument
from app.db.models.users import User


class NotificationSendLog(BaseDocument):
    meta = {
        'collection': 'notificationsendlogs',
        'strict': False
    }

    status = db.StringField(default='')
    title = db.StringField(default='')
    summary = db.StringField(default='')
    receivers = db.ListField(default=[])
    content = db.StringField(default='')
    refId = db.ObjectIdField()
    domain = db.StringField(default='')
    type = db.StringField(default='')
    taskCategory = db.StringField(default='')
    taskType = db.StringField(default='')
    target = db.StringField(default='')
    uid = db.ReferenceField(User)
    endTime = db.DateTimeField()
    startTime = db.DateTimeField()

    def save(self, *args, **kwargs):
        if not self.startTime:
            self.startTime = arrow.utcnow().datetime
        if self.endTime:
            self.endTime = arrow.utcnow().datetime
        return super(NotificationSendLog, self).save(*args, **kwargs)
